package com.cennac.ccandroidtools;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

/**
 * Created by Cennac on 16/2/22.
 */

public class RawprogramFrame  extends JFrame {

    private JMenuBar menuBar;   //菜单栏
    private JOptionPane joptionPane;
    private JFileChooser jFileChooser=new JFileChooser();

    public RawprogramFrame() {
        super();
        this.setSize(600, 400);


//        this.getContentPane().setLayout(null);//设置布局控制器

//        this.getContentPane().setLayout(new FlowLayout());//设置布局控制器

//        this.getContentPane().setLayout(new GridLayout(1,2));//设置布局控制器,需要给出设定的行列数目

//        this.getContentPane().setLayout(new BorderLayout());//设置布局控制器，以North,South,West,East，来控制控件布局

//        this.getContentPane().setLayout(new GridBagLayout());//设置布局控制器

        this.setJMenuBar(this.getMenu());//添加菜单

        this.setTitle("Rawprogram");//设置窗口标题

    }

    /**
     * 菜单的级别JMenuBar->JMenu->JMenuItem
     * 三级都是1：n的关系
     * 最后添加菜单用的SetJMenuBar方法
     * @return 建立好的菜单
     */
    private JMenuBar getMenu(){
        if(menuBar==null){
            menuBar = new JMenuBar();
            //一级菜单
            JMenu mFile = new JMenu();
            mFile.setText("文件");
            JMenu mHelp = new JMenu();
            mHelp.setText("帮助");

            //文件的二级菜单
            JMenuItem iOpen = new JMenuItem();
            iOpen.setText("打开");
            JMenuItem iExit = new JMenuItem();
            iExit.setText("退出");

            //帮助的二级菜单
            JMenuItem iWelcome = new JMenuItem();
            iWelcome.setText("欢迎");
            JMenuItem iVersion = new JMenuItem();
            iVersion.setText("版本信息");

            //为mFile添加二级菜单
            mFile.add(iOpen);
            mFile.add(iExit);

            //为mHelp添加二级菜单
            mHelp.add(iWelcome);
            mHelp.add(iVersion);

            //添加一级菜单mFile,mHelp
            menuBar.add(mFile);
            menuBar.add(mHelp);

            //为二级菜单添加监听事件
            iOpen.addActionListener(new OpenMenu());
            iExit.addActionListener(new ExitMenu());
            iWelcome.addActionListener(new WelcomeMenu());
            iVersion.addActionListener(new VersionMenu());

        }
        return menuBar;
    }

    /**
     * 打开菜单监听器类实现ActionListener接口，主要实现actionPerformed方法
     *
     */
    private class  OpenMenu implements ActionListener{
        public void actionPerformed(ActionEvent e){
            jFileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); //设置只能选择文件
            int state = jFileChooser.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句
            if (state == 1) {
                return;// 撤销则返回
            } else {
                File f = jFileChooser.getSelectedFile();// f为选择到的文件
//                text2.setText(f.getAbsolutePath());
                System.out.println(f.getAbsolutePath());
            }
        }
    }

    /**
     * 退出菜单监听器
     *
     */
    private class ExitMenu implements ActionListener{
        public void actionPerformed(ActionEvent e){
            System.exit(0);
        }
    }

    /**
     * 欢迎菜单监听器
     */
    private class WelcomeMenu implements ActionListener{
        public void actionPerformed(ActionEvent e){
            JOptionPane.showMessageDialog(null,"欢迎使用Rawprogram \nDesign By Cennac","Welcome",JOptionPane.INFORMATION_MESSAGE);//showXXXXDialog需用JOptionPane来获取
        }
    }

    /**
     * 版本菜单监听器
     */
    private class VersionMenu implements ActionListener{
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null,"Beta 1.0 \nDesign By Cennac","版本",JOptionPane.INFORMATION_MESSAGE);//showXXXXDialog需用JOptionPane来获取
        }
    }


}